// .........................................................................
// Title: persistence.do
//
// Implements an analysis of the persistence of the holdings decisions made
// at the time of bond issuance by both insurers and investment funds
// .........................................................................

cap mkdir $tmp/bootstrap

* ---------------------------------------
* Issuance persistence: insurers
* ---------------------------------------

* Panel of insurer shares: insurer positions
use "$tmp/holdings_mf_ins_issuance_trace_universe_refined", clear
foreach var of varlist *holdings* {
    replace `var' = 0 if missing(`var')
}
gen tot_ins_holdings = holdings_ins_health + holdings_ins_life + holdings_ins_pc
gen total_holdings = holdings_mf_foreign + holdings_mf_us + tot_ins_holdings
gen issuance_quarter = qofd(issuance_date)
format %tq issuance_quarter
drop if date_q < issuance_quarter
keep cusip date_q issuance_quarter share_ins_holdings share_ins_issuance total_holdings value_outstanding value_outstanding_mv
gen quarters_from_issuance = date_q - issuance_quarter
gsort cusip quarters_from_issuance
by cusip: egen min_t = min(quarters_from_issuance)
keep if min_t <= 4
replace quarters_from_issuance = quarters_from_issuance - min_t
save "$tmp/shares_issuance_panel", replace

* Prepare passthrough regression sample
use "$tmp/shares_issuance_panel", clear
gen year = year(dofq(issuance_quarter))
mmerge cusip year using "$tmp/issue_ratings_merged_sp_moodys_y", unmatched(m)
mmerge cusip year using "$tmp/amounts_outstanding_y_mv", unmatched(m) update
drop _merge
gen _share_at_issuance = share_ins_issuance if quarters_from_issuance == 0
bys cusip: egen share_at_issuance = max(_share)
drop _share
keep if quarters_from_issuance <= 40
drop if total_holdings < 0
save "$tmp/share_issuance_reg_sample", replace

* Estimate passthrough regressions for insurer holdings of IG bonds
use "$tmp/share_issuance_reg_sample", clear
keep if inlist(rating_cat, "A", "AA", "AAA", "BBB")
statsby _b _se e(r2), clear by(quarters_from_issuance): reg share_ins_issuance share_at_issuance [aw = value_outstanding], robust
keep quarters_from_issuance  _*
gen ci_max = _b_share_at_issuance + 1.96 * _se_share_at_issuance
gen ci_min = _b_share_at_issuance - 1.96 * _se_share_at_issuance
save "$tmp/insurance_issuance_passthroughs", replace

* ---------------------------------------
* Issuance persistence: funds
* ---------------------------------------

* Panel of fund shares: fund positions
use "$tmp/holdings_mf_ins_issuance_trace_universe_refined", clear
foreach var of varlist *holdings* {
    replace `var' = 0 if missing(`var')
}
gen tot_ins_holdings = holdings_ins_health + holdings_ins_life + holdings_ins_pc
gen total_holdings = holdings_mf_foreign + holdings_mf_us + tot_ins_holdings
gen issuance_quarter = qofd(issuance_date)
format %tq issuance_quarter
drop if date_q < issuance_quarter
keep cusip date_q issuance_quarter share_mf_issuance share_mf_holdings total_holdings
gen quarters_from_issuance = date_q - issuance_quarter
gsort cusip quarters_from_issuance
by cusip: egen min_t = min(quarters_from_issuance)
keep if min_t <= 4
replace quarters_from_issuance = quarters_from_issuance - min_t
save "$tmp/shares_issuance_panel_funds", replace

* Prepare regression sample
use "$tmp/shares_issuance_panel_funds", clear
gen year = year(dofq(issuance_quarter))
mmerge cusip year using "$tmp/issue_ratings_merged_sp_moodys_y", unmatched(m)
mmerge cusip year using "$tmp/amounts_outstanding_y_mv", unmatched(m) update
drop _merge
gen _share_at_issuance = share_mf_issuance if quarters_from_issuance == 0
bys cusip: egen share_at_issuance = max(_share)
drop _share
keep if quarters_from_issuance <= 40
drop if total_holdings < 0
save "$tmp/share_issuance_reg_sample_funds", replace

* Estimate passthrough regressions for insurer holdings of IG bonds
use "$tmp/share_issuance_reg_sample_funds", clear
keep if inlist(rating_cat, "A", "AA", "AAA", "BBB")
keep if quarters_from_issuance <= 36
keep if ~missing(share_mf_issuance) & share_mf_issuance <= 1
statsby _b _se e(r2), clear by(quarters_from_issuance): reg share_mf_issuance share_at_issuance [aw = value_outstanding], robust
keep quarters_from_issuance  _*
gen ci_max = _b_share_at_issuance + 1.96 * _se_share_at_issuance
gen ci_min = _b_share_at_issuance - 1.96 * _se_share_at_issuance
save "$tmp/funds_issuance_passthroughs", replace

* ---------------------------------------
* Bootstrap for inference
* ---------------------------------------

* Bootstrap: insurers
forval horizon = 1/36 {
    
    di "Running bootstrap for horizon `horizon'"
    
    * run the bootstrap
    use "$tmp/share_issuance_reg_sample", clear
    keep if inlist(rating_cat, "A", "AA", "AAA", "BBB")
    keep if quarters_from_issuance == `horizon'
    bootstrap e(r2), reps(1000): reg share_ins_issuance share_at_issuance
    
    * store results
    gen r2_bootstrap_se = _se[_bs_1]
    keep quarters_from_issuance r2_bootstrap_se
    keep if _n == 1
    save "$tmp/bootstrap/issuance_persistence_insurers_r2_`horizon'", replace
    
}

* Bootstrap: funds
forval horizon = 1/36 {
    
    di "Running bootstrap for horizon `horizon'"
    
    * run the bootstrap
    use "$tmp/share_issuance_reg_sample_funds", clear
    keep if inlist(rating_cat, "A", "AA", "AAA", "BBB")
    keep if quarters_from_issuance == `horizon'
    bootstrap e(r2), reps(1000): reg share_mf_issuance share_at_issuance
    
    * store results
    gen r2_bootstrap_se = _se[_bs_1]
    keep quarters_from_issuance r2_bootstrap_se
    keep if _n == 1
    save "$tmp/bootstrap/issuance_persistence_funds_r2_`horizon'", replace
    
}

* Append the bootstrap results for all horizons
clear
forval horizon = 1/36 {
    cap append using "$tmp/bootstrap/issuance_persistence_insurers_r2_`horizon'.dta"
}
gen sector = "insurers"
save "$tmp/r2_bootstrap_persistence_insurers", replace

clear
forval horizon = 1/36 {
    cap append using "$tmp/bootstrap/issuance_persistence_funds_r2_`horizon'.dta"
}
gen sector = "funds"
save "$tmp/r2_bootstrap_persistence_funds", replace

* Consolidate bootstrap results
use "$tmp/r2_bootstrap_persistence_insurers", clear
append using "$tmp/r2_bootstrap_persistence_funds"
rename r2_bootstrap_se r2_bse_
reshape wide r2_bse_, i(quarters_from_issuance) j(sector) string
save "$tmp/r2_bootstrap_persistence", replace

* ---------------------------------------
* Plotting results
* ---------------------------------------

* Combine funds and insurers
use "$tmp/insurance_issuance_passthroughs", clear
mmerge quarters_from_issuance using "$tmp/funds_issuance_passthroughs", uname(funds_)
drop _merge

keep if quarters_from_issuance <= 36
mmerge quarters_from_issuance using "$tmp/r2_bootstrap_persistence", unmatched(m)
drop _merge

replace r2_bse_funds = 0 if quarters_from_issuance == 0
replace r2_bse_ins = 0 if quarters_from_issuance == 0

gen r2_ins_max = _eq2_stat_1 + 1.96 * r2_bse_insurers
gen r2_ins_min = _eq2_stat_1 - 1.96 * r2_bse_insurers
gen r2_funds_max = funds__eq2_stat_1 + 1.96 * r2_bse_funds
gen r2_funds_min = funds__eq2_stat_1 - 1.96 * r2_bse_funds

* Plot r-squared
twoway ///
    ( ///
        line _eq2_stat_1 funds__eq2_stat_1 quarters_from_issuance, ///
        graphregion(color(white)) lcolor(red red) lpattern(solid dash) ///
        legend(label(1 "Insurers") label(2 "Funds") label(3 "95% CI (Bootstrap)") order(1 2) cols(3) ) ///
        xtitle("Quarters From Issuance") ylab(0(.2)1, angle(0)) xsize(7.5) xlab(0(4)36) ///
    ) ( ///
        rarea r2_ins_max r2_ins_min quarters_from_issuance, ///
        fcolor(gray%20) lcolor(gray%20) ///
    ) ( ///
        rarea r2_funds_max r2_funds_min quarters_from_issuance, ///
        fcolor(gray%20) lcolor(gray%20) ///
    ), saving("$tmp/persistence_r2_panel.gph", replace) title("(B) R-Squared Values", color(black) size(medium))

* Plot passthrough
twoway ///
    ( ///
        line _b_share_at_issuance funds__b_share_at_issuance quarters_from_issuance, ///
        graphregion(color(white)) lcolor(blue blue) lpattern(solid dash) ///
        legend(label(1 "Insurers") label(2 "Funds") ///
        label(3 "95% CI") order(1 2) cols(3) ) ///
        xtitle("Quarters From Issuance") ylab(0(.2)1, angle(0)) xsize(7.5) xlab(0(4)36) ///
    ) ( ///
        rarea ci_max ci_min quarters_from_issuance, ///
        fcolor(gray%20) lcolor(gray%20) ///
    ) ( ///
        rarea funds_ci_max funds_ci_min quarters_from_issuance, ///
        fcolor(gray%20) lcolor(gray%20) ///
    ), saving("$tmp/persistence_beta_panel.gph", replace) ///
       title("(A) Estimated Passthroughs ({&lambda})", color(black) size(medium))

* Combine the two graph panels and save figure (Figure 8)
gr combine "$tmp/persistence_beta_panel.gph" "$tmp/persistence_r2_panel.gph", ///
    cols(2) graphregion(color(white)) xsize(9) iscale(.95) ycommon

graph export "$graphs/issuance_persistence.pdf", as(pdf) replace
